package com.ruoyi.asset.service.impl;

import com.ruoyi.asset.domain.AstDevice;
import com.ruoyi.asset.domain.AstDeviceRecord;
import com.ruoyi.asset.domain.AstSite;
import com.ruoyi.asset.domain.AstStoreroom;
import com.ruoyi.asset.mapper.AstDeviceMapper;
import com.ruoyi.asset.service.IAstDeviceRecordService;
import com.ruoyi.asset.service.IAstDeviceService;
import com.ruoyi.asset.service.IAstSiteService;
import com.ruoyi.asset.service.IAstStoreroomService;
import com.ruoyi.asset.utils.QRcodeUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 设备管理Service业务层处理
 *
 * @author xunzhe
 * @date 2024-06-07
 */
@Service
public class AstDeviceServiceImpl implements IAstDeviceService {
    @Autowired
    private AstDeviceMapper astDeviceMapper;

    @Autowired
    private IAstDeviceRecordService deviceRecordService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IAstStoreroomService storeroomService;

    @Autowired
    private IAstSiteService siteService;

    /**
     * 查询设备管理
     *
     * @param deviceId 设备管理主键
     * @return 设备管理
     */
    @Override
    public AstDevice selectAstDeviceByDeviceId(Long deviceId) {
        AstDevice device = astDeviceMapper.selectAstDeviceByDeviceId(deviceId);
        if(device.getStoreroomId() != null){
            AstStoreroom astStoreroom = storeroomService.selectAstStoreroomByStoreroomId(device.getStoreroomId());
            device.setStoreroomName(astStoreroom.getStoreroomName());
        }
        if(device.getSiteId() != null){
            AstSite astSite = siteService.selectAstSiteBySiteId(device.getSiteId());
            device.setStoreroomName(astSite.getSiteName());
        }

        return device;
    }

    /**
     * 查询设备管理列表
     *
     * @param astDevice 设备管理
     * @return 设备管理
     */
    @Override
    public List<AstDevice> selectAstDeviceList(AstDevice astDevice) {
        return astDeviceMapper.selectAstDeviceList(astDevice);
    }

    /**
     * 新增设备管理
     *
     * @param astDevice 设备管理
     * @return 结果
     */
    @Override
    @Transactional
    public List<String> insertAstDevice(AstDevice astDevice) throws Exception {
        astDevice.setBatchNumber(QRcodeUtil.suijiNumber());
        astDevice.setCreateTime(DateUtils.getNowDate());
        if(astDevice.getCaozuoStatus()!=null && !astDevice.getCaozuoStatus().equals("")){
            astDevice.setCaozuoRen(astDevice.getCreateBy());
            astDevice.setCaozuoTime(DateUtils.getNowDate());
        }

        List<String> returnList = new ArrayList<>();

        if (astDevice.getDeviceNumber() == null) {
            astDevice.setDeviceQrcode(QRcodeUtil.suijiNumber());
            returnList.add(astDevice.getDeviceQrcode());

            String img = QRcodeUtil.createImg(astDevice.getDeviceQrcode());
            astDevice.setDeviceQrcodeImg(img);
            astDeviceMapper.insertAstDevice(astDevice);
            return returnList;
        }

        List<AstSite> astSiteList = null;
        if(astDevice.getSiteId() != null){
            AstSite site = new AstSite();
            site.setParentId(astDevice.getSiteId());
            astSiteList = siteService.selectAstSiteList(site);
            if(astSiteList.size() != 0 && astSiteList.size() != astDevice.getDeviceNumber().longValue()){
                throw new RuntimeException("站点数量和设备数量不匹配！");
            }
        }

        for (int i = 0; i < astDevice.getDeviceNumber().longValue(); i++) {
            if(astSiteList != null && astSiteList.size() != 0){
                astDevice.setSiteId(astSiteList.get(i).getSiteId());
            }
            astDevice.setDeviceId(null);
            astDevice.setDeviceQrcode(QRcodeUtil.suijiNumber() + "" + i);
            returnList.add(astDevice.getDeviceQrcode());

            String img = QRcodeUtil.createImg(astDevice.getDeviceQrcode());
            astDevice.setDeviceQrcodeImg(img);
            astDeviceMapper.insertAstDevice(astDevice);
        }
        return returnList;
    }


    /**
     * 修改设备管理
     *
     * @param astDevice 设备管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAstDevice(AstDevice astDevice) {
//        if(astDevice.getCaozuoState()!=null && astDevice.getCaozuoState().equals("1")){
//            AstDeviceRecord deviceRecord = new AstDeviceRecord();
//            deviceRecord.setDeviceId(astDevice.getDeviceId());
//            deviceRecord.setCreateBy(ShiroUtils.getSysUser().getLoginName());
//            deviceRecord.setCreateTime(new Date());
//            deviceRecord.setReason(astDevice.getReason());
//            if(astDevice.getStatus().equals("0")){
//                deviceRecord.setStatus("2");
//            }else if(astDevice.getStatus().equals("1")){
//                deviceRecord.setStatus("3");
//            }else if(astDevice.getStatus().equals("2")){
//                deviceRecord.setStatus("1");
//            }
//            deviceRecordService.insertAstDeviceRecord(deviceRecord);
//        }

        return astDeviceMapper.updateAstDevice(astDevice);
    }

    /**
     * 批量删除设备管理
     *
     * @param deviceIds 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteAstDeviceByDeviceIds(String deviceIds) {
        return astDeviceMapper.deleteAstDeviceByDeviceIds(Convert.toStrArray(deviceIds));
    }

    /**
     * 删除设备管理信息
     *
     * @param deviceId 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteAstDeviceByDeviceId(Long deviceId) {
        return astDeviceMapper.deleteAstDeviceByDeviceId(deviceId);
    }

    @Transactional
    public int caozuoDevice(AstDeviceRecord deviceRecord){
        SysUser sysUser = userService.selectUserById(deviceRecord.getUserId());
        deviceRecord.setCreateBy(sysUser.getUserName());
        deviceRecord.setCreateTime(new Date());
        deviceRecord.setSystemDescription("");

        AstDevice astDevice = new AstDevice();
        String status = null;
        //设备是报废状态，增加一条报废操作记录
        if(deviceRecord.getDeviceStatus()!=null && deviceRecord.getDeviceStatus().equals("1")){
            astDevice.setStatus("1");
            status = deviceRecord.getStatus();
            deviceRecord.setStatus("4");
            deviceRecordService.insertAstDeviceRecord(deviceRecord);
        }

        AstDevice device = astDeviceMapper.selectAstDeviceByDeviceId(deviceRecord.getDeviceId());
        astDevice.setDeviceId(deviceRecord.getDeviceId());
        astDevice.setCaozuoRen(sysUser.getUserName());
        astDevice.setCaozuoTime(new Date());
        astDevice.setCaozuoStatus(deviceRecord.getStatus());

        if(deviceRecord.getSiteId() != null){
            if(device.getSiteId() == null || deviceRecord.getSiteId().longValue() != device.getSiteId().longValue()){
                astDevice.setSiteId(deviceRecord.getSiteId());
                deviceRecord.setSystemDescription("原站点id_"+device.getSiteId());
            }
        }

        if(deviceRecord.getStoreroomId() != null){
            if(device.getStoreroomId() == null || deviceRecord.getStoreroomId().longValue() != device.getStoreroomId().longValue()){
                astDevice.setStoreroomId(deviceRecord.getStoreroomId());
                deviceRecord.setSystemDescription(deviceRecord.getSystemDescription()+"原库房id_"+device.getStoreroomId());
            }
        }

        if(status!=null){
            deviceRecord.setStatus(status);
        }

        astDeviceMapper.updateAstDevice(astDevice);
        return deviceRecordService.insertAstDeviceRecord(deviceRecord);
    }

    public AstDevice selectAstDeviceByQrcode(String deviceQrcode){
        return astDeviceMapper.selectAstDeviceByQrcode(deviceQrcode);
    }

    public List<AstDevice> selectAstDeviceBySiteId(Long siteId){
        return astDeviceMapper.selectAstDeviceBySiteId(siteId);
    }


}
